<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="案例名称" prop="caseName">
        <el-input
          v-model="queryParams.caseName"
          placeholder="请输入案例名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属企业" prop="companyName" style="width: 20%">
        <el-select v-model="queryParams.companyName" style="width: 120%" placeholder="请搜索选择所属企业" filterable
                   remote
                   :remote-method="handleSearchCompanies" :loading="loadingCompanies" @change="handleQuery"
                   popper-append-to-body clearable
        >
          <el-option v-for="item in companyList" :key="item.id" :label="item.companyName" :value="item.companyName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['union:case:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['union:case:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['union:case:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col v-if="false" :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['union:case:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="caseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" width="60" align="center" prop="id"/>
      <el-table-column label="案例名称" align="center" prop="caseName"/>
      <el-table-column label="所属企业" align="center" prop="companyName"/>
      <el-table-column label="排序" width="60" align="center" prop="sortOrder"/>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="true"
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['union:case:query']"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['union:case:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['union:case:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改成功案例对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="24">
          <el-col :span="9">
            <el-form-item label="案例名称" prop="caseName">
              <el-input v-model="form.caseName" placeholder="请输入案例名称" :disabled="viewopen"/>
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item label="所属企业" prop="companyName">
              <el-select v-model="form.companyName" style="width: 100%" placeholder="请搜索选择所属企业" filterable
                         remote
                         :remote-method="handleSearchCompanies" :loading="loadingCompanies" :disabled="viewopen"
              >
                <el-option v-for="item in companyList" :key="item.id" :label="item.companyName" :value="item.companyName"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="排序" prop="sortOrder">
              <el-input type="number" v-model="form.sortOrder" placeholder="请输入排序号" :min="1" :default-value="1"
                        :disabled="viewopen"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" v-if="viewopen">
          <el-col :span="12">
            <el-form-item label="创建人" prop="createBy">
              <el-input v-model="form.createBy" :disabled="viewopen"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间" prop="createTime">
              <el-input v-model="form.createTime" :disabled="viewopen"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="更新人" prop="updateBy">
              <el-input v-model="form.updateBy" :disabled="viewopen"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="更新时间" prop="updateTime">
              <el-input v-model="form.updateTime" :disabled="viewopen"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="案例内容" prop="caseContent">
              <editor v-if="!viewopen" ref="caseeditor" :min-height="492" aria-placeholder="请输入案例内容"
                      @on-text-change="updateText"
              />
              <div v-else v-html="form.caseContent"></div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div v-if="!viewopen" slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCompanies
} from '@/api/union/companies'
import { listCase, getCase, delCase, addCase, updateCase, exportCase } from '@/api/union/case'
import Editor from '@/components/Editor/wang';

export default {
  name: 'Case',
  components: { Editor },
  data() {
    return {
      loadingCompanies: false,
      companyList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 成功案例表格数据
      caseList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      viewopen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        caseName: null,
        sortOrder: null,
        companyName: null,
        caseContent: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        caseName: [
          { required: true, message: '案例名称不能为空', trigger: 'blur' },
          { max: 30, message: '案例名称长度不能超过30个字符', trigger: 'blur' }
        ],
        companyName: [
          { required: true, message: '所属企业不能为空', trigger: 'blur' }
        ],
        caseContent: [
          { required: true, message: '案例内容不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    updateText(text){
      this.form.caseContent=text
    },
    /** 查询成功案例列表 */
    getList() {
      this.loading = true
      listCase(this.queryParams).then(response => {
        this.caseList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        caseName: null,
        sortOrder: null,
        companyName: null,
        caseContent: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.viewopen = false
      this.title = '添加成功案例'
      setTimeout(() => {
        this.$refs.caseeditor.initEditor();
      }, 200);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getCase(id).then(response => {
        this.form = response.data
        this.open = true
        this.viewopen = false
        setTimeout(() => {
          this.$refs.caseeditor.setText(this.form.caseContent);
        }, 200);
        this.title = '修改成功案例'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCase(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              }
            })
          } else {
            addCase(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除选中数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delCase(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有成功案例数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportCase(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    },
    handleView(row) {
      this.reset()
      const id = row.id || this.ids
      getCase(id).then(response => {
        this.form = response.data
        this.open = true
        this.viewopen = true
        this.title = '查看成功案例信息'
      })
    },
    handleSearchCompanies(query) {
      if (query !== '') {
        this.loadingCompanies = true
        listCompanies({ companyName: query, pageNum: 1, pageSize: 20 }).then(response => {
          this.companyList = response.rows
          this.loadingCompanies = false
        })
      }
    }
  }
}
</script>
